﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Apps.Console.ViewModel;

namespace Mimoza.Apps.Console.Commands
{
    class AddPolicyCommand: BaseConsoleCommand
    {
        ContainerTreeViewModel m_ViewModel = null;
        public AddPolicyCommand(ContainerTreeViewModel viewModel) 
        {
            m_ViewModel = viewModel;
            m_ViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(this.view_PropertyChanged);
        }

        void view_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (null == m_ViewModel) return;

            Mimoza.ObjectLayer.Container cont = m_ViewModel.Context as Mimoza.ObjectLayer.Container;
            if (cont != null && cont.Type == Mimoza.ObjectLayer.ContainerType.Managed)
            {
                this.Visible = true;
                this.Enabled = true;
            }
            else {
                this.Visible = false;
                this.Enabled = false;
            }
        }

        public override void Execute()
        {
            if (null == m_ViewModel) return;

            Mimoza.ObjectLayer.Policy policy = m_ViewModel.Application.ObjectLayer.PolicyRepository.CreatePolicy();
            policy.IsEnabled = true;

            PolicyWizard wizard = new PolicyWizard(m_ViewModel.Application, policy);
            wizard.Init(Mimoza.GUI.WizardMode.Wizard);
            if (System.Windows.Forms.DialogResult.OK == wizard.Start())
            {
                policy.Parent = m_ViewModel.Context as Mimoza.ObjectLayer.Container;
                policy.DataModificationID = Guid.NewGuid();
                m_ViewModel.Application.ObjectLayer.PolicyRepository.Add(policy);
            }            
        }
    }
}
